from django.http import HttpResponse
from django.shortcuts import render
from .models import Manufacturer, Car
# Create your views here.
from django.views.generic import View, TemplateView, ListView, DetailView, CreateView, UpdateView, DeleteView


def orm(request):
    # m1 = Manufacturer()
    # m1.name="大众"
    # m1.save()
    # m2 = Manufacturer()
    # m2.name = "沃尔沃"
    # m2.save()
    #
    # c1 = Car()
    # c1.name="张三的汽车"
    # c1.manufacturer = m1
    # c1.save()
    # c2 = Car()
    # c2.name = "李四的汽车"
    # c2.manufacturer = m1
    # c2.save()
    return HttpResponse("查看源代码")


class CView(View):
    def get(self, request):
        text = "继承View视图类"
        return render(request, "other/cview.html", context={"text": text})

    def post(self, request):
        return HttpResponse("post请求成功")


class CTemplateView(TemplateView):
    extra_context = {"text": "继承TemplateView视图类"}
    template_name = "other/cview.html"

    def post(self, request):
        return HttpResponse("post请求成功")


class CListView(ListView):
    # 三选一
    model = Manufacturer
    # queryset = Manufacturer.objects.all()
    # def get_queryset(self):
    #     return Manufacturer.objects.all()

    # 二选一
    template_name = "other/list.html"

    # def get_template_names(self):
    #     return ["other/list.html"]

    extra_context = {"text": "继ListTemplateView视图类"}


class CDetailView(DetailView):
    queryset = Manufacturer.objects.all()

    pk_url_kwarg = "id"

    # slug_url_kwarg = "id"
    # slug_field = "id"


    context_object_name = "m"

    def get_template_names(self):
        return ("other/detail.html")
